<?php

/*
 * Copyright 2014 gg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Wechat\Controller;
use Think\Controller;
/**
 * Description of LoginController
 *
 * @author gg
 */
class LoginController extends Controller{
    public function _before_index(){
        header("Content-type: text/html; charset=UTF-8");
    }

    public function index(){
        $key = I('request.key');
        require_once './jwlogin/authentica.html';
    }
    
    public function login(){
        $query = new \Wechat\Model\DataqueryModel();
        $stu = new \Wechat\Model\StudentModel();
        $username =  I('request.username',"");
        $passwd =  I('request.password',"");
        $key =  I('request.key',"");
        if ( ($username!="")and($passwd!="")and($key!="") ){
            $chkkey = $stu->chkPublicKey($key);
            if ($chkkey!=false){
                $this->ajaxReturn(array('flag'=>FALSE,'content'=>"已经绑定过"));
                exit();
            }
            $stu = new \Wechat\Model\StudentModel();
            $tmp = $query->login($username,$passwd);
            if ($tmp){
                $stu->create(array('stunumber'=>$username,'passwd'=>$passwd,'publickey'=>$key,'status'=>1));
                $stu->add();
//                $username.$passwd.$key
                $this->ajaxReturn(array('flag'=>TRUE,'content'=>"绑定成功"));
            }
            else $this->ajaxReturn(array('flag'=>FALSE,'content'=>"用户名或密码有误$tmp"));
        }else{
            $this->ajaxReturn(array('flag'=>FALSE,'content'=>"信息有误，请点击返回按钮回到微信重新绑定"));
        }
        $this->ajaxReturn(array('flag'=>FALSE,'content'=>"信息有误，请点击返回按钮回到微信重新绑定"));
    }
    
    public function test(){
        $query = new \Wechat\Model\DataqueryModel();
        $query->setKey('test1');
//        $m = date("m");
//        $y = date("Y");
//        $xq = "";
//        if ($m < 3) {
//            $xq = "".($y-1)."-$y-1";
//        }elseif($m<8){
//            $xq = "".($y-1)."-$y-2";
//        }else{
//            $xq = "".($y)."-".($y+1)."-1";
//        }
//        echo $xq;
//        $stu = new \Wechat\Model\StudentModel();
//        $stu->unBind('ogqZFt6fP_WzilD0O5m3CgyU5d_4');
//        $query->login('10065030','221100g0');
//        $query->jwCheck();
//        $query->getMark();
//        echo $query->getClass();
        
        
        $class = new \Wechat\Model\ClassModel();
        $class->saveClass("ghddshfdgfsh",'123456');
    }
    
    
//    public function test1(){
//        $fp = fopen(TEMP_PATH."/tmp/exam", 'r');
//        $content = fread($fp, 100000000);
//        $optStr = "考试信息：";
//        require_once './Public/phpQuery.php';
//        \phpQuery::newDocumentFile("http://59.68.63.164/scuecWechat/App/Runtime/Temp/tmp/exam");
//        $trs = pq("tr.t_con");
//        foreach ($trs as $tr) {
//            $trText = pq($tr)->text();
//            $arr = mb_split('\n', $trText);
//            $num =  count($arr);
//            if ($num==16){
//                $optStr.="课程:".  trim($arr[2])."\n".
//                            "任课老师:".trim($arr[4])."\n".
//                            "考试时间:".trim($arr[8])."\n".
//                            "考试地点:".trim($arr[9])."\n".
//                            "试卷类型:".trim($arr[10])."\n".
//                            "考试状态:".trim($arr[12])."\n".
//                            "------\n";
//            }else{
//                echo "==>\n";
//                $optStr.="课程:".  trim($arr[2])."\n".
//                        "考试地点:".trim($arr[4])."\n"."------\n";
//            }
//        }
//        echo $optStr;
//    }
    
    
    
    
}
